package by.semerikov.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import by.semerikov.parts.Log;

public class TextParserFromFile {
	private TextLineParser textLineParser = new TextLineParser();
	private String fileName;
	private List<Log> list;
	
	
	public TextParserFromFile(String fileName) {
		this.fileName = fileName;
	}
	
	public List<Log> getLogs()
	{
		if(list == null)
		{
			list = new ArrayList<>();
			File file = new File(fileName);
			try {
				FileReader fileReader = new FileReader(file);
				BufferedReader reader = new BufferedReader(fileReader);
				 String line;
				 while ((line = reader.readLine()) != null) {
					 list.add(textLineParser.getLog(line));
				 }
				 reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

}
